package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags={"员工接口"})
@RestController //@Controller和@ResponseBody的组合注解
@RequestMapping("/shop") //暴露公共的路径
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){

        //调用业务层
        shopService.settlement(shop);
        //返回
        return AjaxResult.createSuccess();
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @GetMapping("/jihuo/{984b4b68f1b241148e3a68630deefb1f}")
    public AjaxResult jihuo(@PathVariable("984b4b68f1b241148e3a68630deefb1f") String uuid){

        //调用业务层
        shopService.jihuo(uuid);
        //返回
        return AjaxResult.createSuccess();
    }



    /**
     * 分页接口
     * 1.请求方式 2.请求路径 3.分析参数接收 4.返回值(total:总条数 list:当前页数据)
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value="查询分页", notes="查询员工分页列表")
    public AjaxResult findPage(@RequestBody ShopQuery query){

        //调用service层分页方法
        return AjaxResult.createSuccess(shopService.findPage(query));
    }


    @GetMapping("/findAll")
    @ApiOperation(value="查询员工", notes="查询员工列表")
    public AjaxResult findAll(){
        //1.查询
        List<Shop> shops = shopService.findAll();
        //2.返回数据
        return AjaxResult.createSuccess(shops);
    }


    /**
     * 新增/修改
     * @return
     */
    @PostMapping
    @ApiOperation(value="新增或者修改", notes="没有id就是新增，有id就是修改")
    public AjaxResult saveOrUpdate(@ApiParam(value = "新增员工参数", required = true)@RequestBody Shop shop){

        try {
            //新增/修改
            shopService.saveOrUpdate(shop);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError("操作异常，联系管理员李裕");
        }
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value="删除一个员工", notes="就是删除")
    public AjaxResult delete(@ApiParam(value = "主键id", required = true) @PathVariable("id") Long id){

        try {
            //新增/修改
            shopService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，联系管理员李裕");
        }
    }

}
